//
//  CYVistorLoginView.swift
//  CYSinaWB
//
//  Created by mac on 16/2/16.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol VistorLoginViewDelegate: NSObjectProtocol {
    
    optional func userWillLogin()
    
    func userWillRegister()
}

class CYVistorLoginView: UIView {
    //声明代理对象  一定要使用weak 防止循环引用
    weak var visitorLoginViewDelegate: VistorLoginViewDelegate?

    //自定义访客视图
    //默认的构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //点击事件监听
    @objc private func registerBtnClick() {
        visitorLoginViewDelegate?.userWillRegister()
    }
    @objc private func loginBtnClick() {
        visitorLoginViewDelegate?.userWillLogin?()
    }
    
    private func setupUI() {
        addSubview(circleIcon)
        addSubview(backImage)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(self.snp_centerX)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.width.equalTo(100)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.width.equalTo(100)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
        }
        backImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
//        let value: CGFloat = 236 / 255.0
//        self.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
        
    }
    //一旦重写了 init(frame方法之后 系统会默认该视图会通过手写代码创建
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置页面信息
    func setupInfoWith(tipText: String,imageName: String?) {
        tipLabel.text = tipText
        if imageName != nil {
            //不是首页
        circleIcon.image = UIImage(named: imageName!)
            largeHouse.hidden = true
            //将circleIcon移到顶层
            self.bringSubviewToFront(circleIcon)
        }else {
            //是首页
            startAnimation()
        }
    }
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 20
        //处于不活跃状态不移除
        anim.removedOnCompletion = false
        circleIcon.layer.addAnimation(anim, forKey: nil)
    }
    
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var  circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var  largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var  tipLabel: UILabel = UILabel(text: "关注一些人,回这里看看有什么惊喜", fontSize: 14, textColor: UIColor.lightGrayColor())
    
//    {
//        let l = UILabel()
//        l.text = "关注一些人,回这里看看有什么惊喜"
//        l.font = UIFont.systemFontOfSize(14)
//        l.textColor = UIColor.lightGrayColor()
//        l.numberOfLines = 0
//        l.textAlignment = NSTextAlignment.Center
//        l.sizeToFit()
//        return l
//    }()
    
    //设置登录按钮
    private lazy var loginBtn: UIButton = UIButton(backName: "common_button_white_disable", title: "登录", textColor: UIColor.darkGrayColor())
    
//    {
//        let btn = UIButton()
//        btn.setTitle("登录", forState: .Normal)
//        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        return btn
//    }()
    private lazy var registerBtn: UIButton = UIButton(backName: "common_button_white_disable", title: "注册", textColor: themeColor)
//    {
//        let btn = UIButton()
//        btn.setTitle("注册", forState: UIControlState.Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
//        return btn
//    }()

}
